<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" align-center="true" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-form-item label="维修人员" prop="user_id">
                <!-- <el-select v-model="form.user_id"  filterable style="width: 100%" placeholder="请选择维修人员" clearable>
                    <el-option v-for="item in humList" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select> -->
                <div style="display:flex;width:100%">
                    <el-input v-model="name" placeholder="维修人员" @input="input" clearable style="width:100%"></el-input>
                    <el-button @click="check">选择人员</el-button>
                </div>
            </el-form-item>
            <el-form-item label="转单说明" prop="illu">
                <el-input v-model="form.illu" placeholder="转单说明" @input="input" clearable style="width:100%"></el-input>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <hum-list v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false" draggable></hum-list>
</template>

<script>
import humList from './humList.vue';
export default {
  components: { humList },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增维修工单',
				edit: '维修转单',
				show: '查看'
			},
            dialog: {
				save: false,
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				user_id: "",
                illu:"",
			},
            name:"",
			//验证规则
			rules: {
				// ware_name: [
				// 	{ required: true, message: '请输入维修工单名称' }
				// ],
				// ware_code: [
				// 	{ required: true, message: '请输入维修工单编号' }
				// ],
			},
            humList:[],//人员
		}
	},
	mounted() {
        this.getHum()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					res = await this.$API.device.deviceTask.edit.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("转单成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
		},
        // 人员
        async getHum(){
            var res = await this.$API.all.getAdmin.get({is_page:0});
            this.humList = res.data
        },
        // 选择人员
        check(){
            this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
        },
        handleSuccess(data) {
            this.form.user_id = data[0].id
            this.name = data[0].name
        },
	}
}
</script>

<style></style>
